package com.lzxuni.modules.demo.controller;


import com.alibaba.fastjson.JSON;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.UuidUtil;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.FileEntity;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.service.FileEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@RestController
@RequestMapping("/LR_CodeGeneratorModule/attachment")
public class AttchmentDemoController extends BaseController {
	@Autowired
	private FileEntityService fileEntityService;
	// 列表
	@RequestMapping("/Index")
	public ModelAndView list() {
		ModelAndView mv = new ModelAndView("/LR_CodeGeneratorModule/attachment/index");
		return mv;
	}
	// 列表
	@RequestMapping("/index_o")
	public Object listO(String pagination,
							  String queryJson) {
		PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
		FileEntity fileEntity = JSON.parseObject(queryJson, FileEntity.class);
		if (fileEntity == null) {
			fileEntity = new FileEntity();
		}
		PageData pageData = getPageData(fileEntityService.queryPage(pageParameter, fileEntity));
		return R.ok(pageData);
	}

	// 新增,修改
	@RequestMapping("/insert_v.html")
	public ModelAndView insert() throws Exception{
		ModelAndView mv = new ModelAndView("/LR_CodeGeneratorModule/attachment/Form");
		return mv;
	}
	// 新增,修改 处理
	@RequestMapping("/insert_o.html")
	public Object insertDo(FileEntity fileEntity,String pictures) throws Exception {

		if(StringUtils.isEmpty(fileEntity.getId())){
			fileEntity.setId(UuidUtil.get32UUID());
			fileEntity.setCreateTime(new Date());
			fileEntityService.insert(pictures,fileEntity.getYwId(),fileEntity.getType(),fileEntity.getYwType(),fileEntity.getDescrption());
			return R.ok("新增成功");
		}else{
			fileEntityService.deleteByYwId(fileEntity.getYwId());
			fileEntityService.insert(pictures,fileEntity.getYwId(),fileEntity.getType(),fileEntity.getYwType(),fileEntity.getDescrption());
			return R.ok("修改成功");
		}
	}
	//删除
	@RequestMapping("/delete_o.html")
	public Object delete(@RequestParam("keyValue")String id){
		fileEntityService.deleteById(id);
		return  R.ok("删除成功");
	}

}